package GUI;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
/**
 * A public class that creates the InfoPanel at the top of the GUI.
 * @author abrahamsantini
 *
 */
public class InfoPanel extends JPanel {
	/**
	 * A JTextField that allows the user to enter the row height in pixels
	 */
	private JTextField my_rowText;
	/**
	 * A JTextField that allows the user to enter the column width in pixels
	 */
	private JTextField my_columnText;
	/**
	 * A JLabel that will display to selected cell.
	 */
	private JLabel my_cellLabel = new JLabel("NO FILE");
	/**
	 * A JText field that displays the formula for the selected cell.
	 */
	private JTextField my_cellText;
	/**
	 * A JPanel to hold the contents of the InfoPanel
	 */
	private JPanel my_panel;
	/**
	 * A reference to the GUI that this will be a part of.
	 */
	private GUI my_gui;
	/**
	 * Public constructor for creating an InfoPanel
	 * @param the_gui The GUI that the InfoPanel will be a component of.
	 */
	public InfoPanel(GUI the_gui) {
		my_gui = the_gui;
		my_panel = new JPanel();
		//my_panel.setMinimumSize(my_min_size);
		my_panel.setLayout(new BoxLayout(my_panel, BoxLayout.X_AXIS));
		my_panel.add(createSizePanel());
		my_panel.add(createCellPanel());
		my_panel.add(Box.createRigidArea(new Dimension(5,0)));
		my_panel.add(Box.createHorizontalGlue());
		my_panel.add(Box.createRigidArea(new Dimension(5,0)));
	}
	/**
	 * Create the panel that holds the resizing options.
	 * @return tempPanel The panel that hold the resizing components.
	 */
	private JPanel createSizePanel() {
		JPanel tempPanel = new JPanel();
		tempPanel.setMaximumSize(new Dimension(20, 100));
		tempPanel.setLayout(new BoxLayout(tempPanel, BoxLayout.X_AXIS));
		JLabel rowLabel = new JLabel("Row Height");
		tempPanel.add(rowLabel);
		my_rowText = new JTextField(3);
		my_rowText.setText(Integer.toString(my_gui.getRowHeight()));
		tempPanel.add(my_rowText);
		JLabel columnLabel = new JLabel("Column Width");
		tempPanel.add(columnLabel);
		my_columnText = new JTextField(4);
		my_columnText.setText(Integer.toString(my_gui.getColumnWidth()));
		tempPanel.add(my_columnText);
		JButton resizeButton = new JButton("Resize");
		resizeButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				my_gui.setRowHeight(Integer.parseInt(my_rowText.getText()));
				my_gui.setColumnWidth(Integer.parseInt(my_columnText.getText()));
				SpreadSheetFrame tempSheet;
				my_gui.getDesktop().removeAll();
				for(int i = 1; i <= my_gui.getSpreadSheets().size(); i++ ){
					tempSheet = my_gui.getSpreadSheets().remove(0);
					if (!tempSheet.isClosed()) {
						my_gui.getSpreadSheets().add(new SpreadSheetFrame(tempSheet.getRows(), 
								tempSheet.getColumns(), my_gui, tempSheet.getTitle(),
								tempSheet.getSpreadsheet()));
						my_gui.getDesktop().add(my_gui.getSpreadSheets()
								.get(my_gui.getSpreadSheets().size()-1).getFrame());
					}	
				}
//				my_gui.getDesktop().repaint();
			my_gui.getFrame().repaint();
			}
		});
		tempPanel.add(resizeButton);
		
		return tempPanel;
	}
	/**
	 * Creates the JPanel that hold the Cell information.
	 * @return tempPanel The panel that holds the Cell information.
	 */
	private JPanel createCellPanel() {
		JPanel tempPanel = new JPanel();
		//tempPanel.setMaximumSize(new Dimension(20, 200));
		tempPanel.setLayout(new BoxLayout(tempPanel, BoxLayout.LINE_AXIS));
		tempPanel.add(my_cellLabel);
		my_cellText = new JTextField(10);
		tempPanel.add(my_cellText);
		return tempPanel;
	}
	/**
	 * Gets the string entered in my_rowText.
	 * @return my_rowText.getText() The string representation of the row height.
	 */
	public String getRowHeight() {
		return my_rowText.getText();
	}
	/**
	 * Gets the string entered in my_columnText.
	 * @return my_columnText.getText() The string representation of the column width.
	 */
	public String getColumnHeight() {
		return my_columnText.getText();
	}
	/**
	 * Gets the panel that holds the InfoPanel components.
	 * @return my_panel The JPanel that holds the InfoPanel components.
	 */
	public JPanel getPanel() {
		return my_panel;
	}
	/**
	 * Sets the Cell label text.
	 * @param the_text The text that will be displayed in my_cellLabel.
	 */
	public void setCellLabel(String the_text) {
		my_cellLabel.setText(the_text);
	}
	/**
	 * Sets the text that will be displayed in my_cellText.
	 * @param the_text This should be the formula of the currently selected cell.
	 */
	public void setCellText(String the_text) {
		my_cellText.setText(the_text);
	}
	
}
